package com.probiz.estoremf.customer.web.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.common.model.customer.Consult;
import com.probiz.estore.common.model.customer.Feedback;
import com.probiz.estore.common.model.system.AppUser;
import com.probiz.estore.core.controller.GenericStoreFrontController;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.customer.model.FeedbackSearchCriteria;
import com.probiz.estore.customer.service.FeedbackManager;
import com.probiz.estore.util.ipwry.IPSeeker;
import com.probiz.estore.webapp.util.RequestContext;
import com.probiz.estore.webapp.util.RequestUtil;

public class FeedbackFrontController extends		GenericStoreFrontController<Feedback> {
	private FeedbackManager feedbackManager;
	protected void initController() throws Exception {
		mgr = feedbackManager;
	}
	
	public void setFeedbackManager(FeedbackManager feedbackManager) {
		this.feedbackManager = feedbackManager;
	}

	public ModelAndView defaultAction(HttpServletRequest request,
			HttpServletResponse response) {

		// -----start:获得反馈------
		SearchCriteria searchCriteria = this.createSearchCriteria(request);
		FeedbackSearchCriteria feedbackSearchCriteria = new FeedbackSearchCriteria();
		bindOnly(request, feedbackSearchCriteria,"feedbackSearchCriteria");
		feedbackSearchCriteria.setStatus(Consult.STATUS_SHOW);
		List<Feedback>  feedbackList =feedbackManager.searchByCriteriaForController(feedbackSearchCriteria, searchCriteria);
		// -----end:  获得反馈------
		
	
		ModelAndView mv = new ModelAndView("customer/feedbackList");
		mv.addObject("feedbackList",feedbackList);
		mv.addObject("feedbackSearchCriteria",feedbackSearchCriteria);
		mv.addObject("currUser",(AppUser) RequestContext.getCurrentUser());
		return mv;
	}
	
	/**
	 * 发表反馈
	 * @param request
	 * @param response
	 * @return
	 */
	public ModelAndView addFeedback(HttpServletRequest request,HttpServletResponse response){
		Feedback feedback = new Feedback();
		feedback.setMessage(RequestUtil.getParameterNullSafe(request, "message"));
		feedback.setCustomerName(request.getParameter("customerName"));
		feedback.setContact(request.getParameter("contact"));
		feedback.setCreateTime(new Date());
		feedback.setCustomer((AppUser) RequestContext.getCurrentUser());
		feedback.setRemoteIp(request.getRemoteAddr());
		feedback.setAddress(IPSeeker.getInstance().getAddress(feedback.getRemoteIp()));
		feedback.setIsRead(Feedback.STATUS_UNREAD);
		feedback.setStatus(Feedback.STATUS_UNSHOW);
		feedback.setReplyCount(Integer.valueOf(0));
		feedback.setOnShowReplyCount(Integer.valueOf(0));
		feedbackManager.save(feedback);
		ModelAndView mv = new ModelAndView("customer/addFeedbackSuccess");
		return mv;
		
	}
	
	
}
